﻿using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceCompanyService
    {
        public InsuranceCompany Load(long id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }

        public InsuranceCompany Load(string companyName, string parentCompanyName)
        {
            return LoadEntities(o => o.CompanyName.Equals(companyName) && o.ParentCompanyName.Equals(parentCompanyName)).FirstOrDefault();
        }

        public IQueryable<InsuranceCompany> GetInsuranceCompanyAll(bool onlyParent)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceCompany>().And(o => o.IsAvailable);
            if (onlyParent)
                expressionWhere = expressionWhere.And(o => o.ParentID == 0);//只有两级
            return LoadEntities(expressionWhere);
        }

        public bool IsExist(string insuranceCompanyName, long id = 0)
        {
            var expressionWhere =
                PredicateExtensions.True<InsuranceCompany>().And(o => o.CompanyName.Equals(insuranceCompanyName));
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            return LoadEntities(expressionWhere).Any();
        }
        public InsuranceCompany GetCompanyByParentId(long parentId)
        {
            return LoadEntities(o => o.ID==parentId).FirstOrDefault();
        }
        public IQueryable<InsuranceCompany> GetPagingList(BaseQueryParams model, out int total)
        {
            var expressionWhere =
               PredicateExtensions.True<InsuranceCompany>();
            if (!string.IsNullOrEmpty(model.Keyword))
            {
                expressionWhere = expressionWhere.And(o => o.CompanyName.Contains(model.Keyword));
            }
            return LoadPagerEntities<InsuranceCompany>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
    }
}